//
//  TabBarController.m
//  TabBarController
//
//  Created by mac on 2018/9/11.
//  Copyright © 2018年 YangYang. All rights reserved.
//

#import "TabBarController.h"
#import "NavigationController.h"
#import "ViewController.h"
#import "LanguageManager.h"
//#import "TBCityIconFont.h"
//#import "UIImage+TBCityIconFont.h"
//
//#import "HomeViewController.h"
//#import "MusicViewController.h"
//#import "MyViewController.h"

@interface TabBarController ()

@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTabBarControllers];
    [self setupTabBarAppearance];
    
    // 监听语言改变通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(languageChanged:)
                                                 name:@"LanguageChanged"
                                               object:nil];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setupTabBarControllers {
    LanguageManager *manager = [LanguageManager sharedManager];
    
    // 首页
    ViewController *homeVC = [[ViewController alloc] init];
    NavigationController *homeNC = [[NavigationController alloc] initWithRootViewController:homeVC];
    homeNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:[manager localizedStringForKey:@"首页"]
                                                      image:[[UIImage imageNamed:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] 
                                              selectedImage:[[UIImage imageNamed:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    homeVC.tabBarItem.tag = 1;
    
    // 视频
    ViewController *videoVC = [[ViewController alloc] init];
    NavigationController *videoNC = [[NavigationController alloc] initWithRootViewController:videoVC];
    videoNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:[manager localizedStringForKey:@"视频"]
                                                       image:[[UIImage imageNamed:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] 
                                               selectedImage:[[UIImage imageNamed:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    videoVC.tabBarItem.tag = 2;
    
    // 音乐
    ViewController *musicVC = [[ViewController alloc] init];
    NavigationController *musicNC = [[NavigationController alloc] initWithRootViewController:musicVC];
    musicNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:[manager localizedStringForKey:@"音乐"]
                                                       image:[[UIImage imageNamed:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] 
                                               selectedImage:[[UIImage imageNamed:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    musicVC.tabBarItem.tag = 3;
    
    // 我的
    ViewController *myVC = [[ViewController alloc] init];
    NavigationController *myNC = [[NavigationController alloc] initWithRootViewController:myVC];
    myNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:[manager localizedStringForKey:@"我的"]
                                                    image:[[UIImage imageNamed:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] 
                                            selectedImage:[[UIImage imageNamed:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    myVC.tabBarItem.tag = 4;
    
    self.viewControllers = @[homeNC, videoNC, musicNC, myNC];
}

- (void)setupTabBarAppearance {
    // 设置TabBar选中和未选中状态的颜色
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} 
                                             forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor]} 
                                             forState:UIControlStateNormal];
}

- (void)languageChanged:(NSNotification *)notification {
    [self updateTabBarLanguage];
}

- (void)updateTabBarLanguage {
    LanguageManager *manager = [LanguageManager sharedManager];
    
    // 更新所有TabBarItem的标题
    if (self.viewControllers.count >= 4) {
        UINavigationController *homeNC = self.viewControllers[0];
        homeNC.tabBarItem.title = [manager localizedStringForKey:@"首页"];
        
        UINavigationController *videoNC = self.viewControllers[1];
        videoNC.tabBarItem.title = [manager localizedStringForKey:@"视频"];
        
        UINavigationController *musicNC = self.viewControllers[2];
        musicNC.tabBarItem.title = [manager localizedStringForKey:@"音乐"];
        
        UINavigationController *myNC = self.viewControllers[3];
        myNC.tabBarItem.title = [manager localizedStringForKey:@"我的"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
